load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_proto_library")
load("//bazel:data2c.bzl", "data2c_gen2", "data2c_gen2_opt")
load("//bazel:closure_compiler.bzl", "closure_compiler_gen", "closure_compiler_without_dependency_mode")

licenses(["notice"])  # Apache 2

closure_compiler_gen(
    name = "critical_css_loader_dbg",
    entry_points = [
        "goog:pagespeed.CriticalCssLoader",
    ],
    js_dir = "rewriter",
    js_includes = [
        "net/instaweb/js/js_utils.js",
    ],
    js_src = "net/instaweb/rewriter/critical_css_loader.js",
    opt = False,
)

closure_compiler_gen(
    name = "critical_css_loader_opt",
    entry_points = [
        "goog:pagespeed.CriticalCssLoader",
    ],
    js_dir = "rewriter",
    js_includes = [
        "net/instaweb/js/js_utils.js",
    ],
    js_src = "net/instaweb/rewriter/critical_css_loader.js",
    opt = True,
)

closure_compiler_gen(
    name = "critical_images_beacon_dbg",
    entry_points = [
        "goog:pagespeed.CriticalImages",
    ],
    js_dir = "rewriter",
    js_includes = [
        "net/instaweb/js/js_utils.js",
    ],
    js_src = "net/instaweb/rewriter/critical_images_beacon.js",
    opt = False,
)

closure_compiler_gen(
    name = "critical_images_beacon_opt",
    entry_points = [
        "goog:pagespeed.CriticalImages",
    ],
    js_dir = "rewriter",
    js_includes = [
        "net/instaweb/js/js_utils.js",
    ],
    js_src = "net/instaweb/rewriter/critical_images_beacon.js",
    opt = True,
)

closure_compiler_gen(
    name = "responsive_js_dbg",
    entry_points = [
        "goog:pagespeed.Responsive",
    ],
    js_dir = "rewriter",
    js_src = "net/instaweb/rewriter/responsive_js.js",
    opt = False,
)

closure_compiler_gen(
    name = "responsive_js_opt",
    entry_points = [
        "goog:pagespeed.Responsive",
    ],
    js_dir = "rewriter",
    js_src = "net/instaweb/rewriter/responsive_js.js",
    opt = True,
)

closure_compiler_without_dependency_mode(
    name = "without_dependency",
    externs = [
        "net/instaweb/js/externs.js",
    ],
    js_dir = "rewriter",
    js_src = [
        "net/instaweb/rewriter/add_instrumentation.js",
        "net/instaweb/rewriter/client_domain_rewriter.js",
        "net/instaweb/rewriter/dedup_inlined_images.js",
        "net/instaweb/rewriter/defer_iframe.js",
        "net/instaweb/rewriter/delay_images_inline.js",
        "net/instaweb/rewriter/deterministic.js",
        "net/instaweb/rewriter/extended_instrumentation.js",
    ],
    opt = False,
)

closure_compiler_without_dependency_mode(
    name = "without_dependency",
    externs = [
        "net/instaweb/js/externs.js",
    ],
    js_dir = "rewriter",
    js_src = [
        "net/instaweb/rewriter/add_instrumentation.js",
        "net/instaweb/rewriter/client_domain_rewriter.js",
        "net/instaweb/rewriter/dedup_inlined_images.js",
        "net/instaweb/rewriter/defer_iframe.js",
        "net/instaweb/rewriter/delay_images_inline.js",
        "net/instaweb/rewriter/deterministic.js",
        "net/instaweb/rewriter/extended_instrumentation.js",
    ],
    opt = True,
)

closure_compiler_without_dependency_mode(
    name = "with_dependency",
    externs = [
        "net/instaweb/js/externs.js",
    ],
    js_dir = "rewriter",
    js_includes = [
        "net/instaweb/js/js_utils.js",
    ],
    js_src = [
        "net/instaweb/rewriter/critical_css_beacon.js",
        "net/instaweb/rewriter/lazyload_images.js",
        "net/instaweb/rewriter/delay_images.js",
        "net/instaweb/rewriter/js_defer.js",
        "net/instaweb/rewriter/local_storage_cache.js",
    ],
    opt = False,
)

closure_compiler_without_dependency_mode(
    name = "with_dependency",
    externs = [
        "net/instaweb/js/externs.js",
    ],
    js_dir = "rewriter",
    js_includes = [
        "net/instaweb/js/js_utils.js",
    ],
    js_src = [
        "net/instaweb/rewriter/critical_css_beacon.js",
        "net/instaweb/rewriter/lazyload_images.js",
        "net/instaweb/rewriter/delay_images.js",
        "net/instaweb/rewriter/js_defer.js",
        "net/instaweb/rewriter/local_storage_cache.js",
    ],
    opt = True,
)

genrule(
    name = "csp_directive_gperf",
    srcs = ["csp_directive.gperf"],
    outs = ["csp_directive.gp.cc"],
    cmd = ("gperf -m10 $< > $@"),
)

genrule(
    name = "rewrite_filter_names_gperf",
    srcs = ["rewrite_filter_names.gperf"],
    outs = ["rewrite_filter_names.gp.cc"],
    cmd = ("gperf -m10 $< > $@"),
)

proto_library(
    name = "rewriter_protos",
    srcs = [
        "cached_result.proto",
        "critical_images.proto",
        "critical_keys.proto",
        "dependencies.proto",
        "flush_early.proto",
        "input_info.proto",
        "rendered_image.proto",
        "static_asset_config.proto",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "//net/instaweb/spriter:spriter_protos",
        "//pagespeed/kernel/http:http_proto",
    ],
)

cc_proto_library(
    name = "rewriter_protos_cc",
    deps = [
        ":rewriter_protos",
    ],
)

data2c_gen2(
    name = "data2c_generated",
    srcs = [
        "add_instrumentation",
        "client_domain_rewriter",
        "critical_css_beacon",
        "critical_css_loader",
        "critical_images_beacon",
        "dedup_inlined_images",
        "defer_iframe",
        "delay_images",
        "delay_images_inline",
        "deterministic",
        "extended_instrumentation",
        "js_defer",
        "lazyload_images",
        "local_storage_cache",
        "responsive_js",
    ],
)

data2c_gen2_opt(
    name = "data2c_generated",
    srcs = [
        "add_instrumentation",
        "client_domain_rewriter",
        "critical_css_beacon",
        "critical_css_loader",
        "critical_images_beacon",
        "dedup_inlined_images",
        "defer_iframe",
        "delay_images",
        "delay_images_inline",
        "deterministic",
        "extended_instrumentation",
        "js_defer",
        "lazyload_images",
        "local_storage_cache",
        "responsive_js",
    ],
)

#"js_minify_main.cc",
#"css_minify_main.cc",

cc_library(
    name = "html_minifier_main_lib",
    srcs = [
        "html_minifier_main.cc",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":rewriter",
    ],
)

cc_library(
    name = "rewriter",
    srcs = [
        "add_head_filter.cc",
        "add_ids_filter.cc",
        "add_instrumentation_filter.cc",
        "association_transformer.cc",
        "base_tag_filter.cc",
        "beacon_critical_images_finder.cc",
        "cache_extender.cc",
        "cacheable_resource_base.cc",
        "collect_dependencies_filter.cc",
        "common_filter.cc",
        "config/measurement_proxy_rewrite_options_manager.cc",
        "config/rewrite_options_manager.cc",
        "critical_css_beacon_filter.cc",
        "critical_finder_support_util.cc",
        "critical_images_beacon_filter.cc",
        "critical_images_finder.cc",
        "critical_selector_filter.cc",
        "critical_selector_finder.cc",
        "csp.cc",
        "css_absolutify.cc",
        "css_combine_filter.cc",
        "css_filter.cc",
        "css_hierarchy.cc",
        "css_image_rewriter.cc",
        "css_inline_filter.cc",
        "css_inline_import_to_link_filter.cc",
        "css_minify.cc",
        "css_move_to_head_filter.cc",
        "css_outline_filter.cc",
        "css_resource_slot.cc",
        "css_summarizer_base.cc",
        "css_tag_scanner.cc",
        "css_url_counter.cc",
        "css_url_encoder.cc",
        "css_url_extractor.cc",
        "css_util.cc",
        "data_url_input_resource.cc",
        "debug_filter.cc",
        "decode_rewritten_urls_filter.cc",
        "dedup_inlined_images_filter.cc",
        "defer_iframe_filter.cc",
        "delay_images_filter.cc",
        "dependency_tracker.cc",
        "deterministic_js_filter.cc",
        "device_properties.cc",
        "dom_stats_filter.cc",
        "domain_lawyer.cc",
        "domain_rewrite_filter.cc",
        "downstream_cache_purger.cc",
        "downstream_caching_directives.cc",
        "experiment_matcher.cc",
        "experiment_util.cc",
        "fake_filter.cc",
        "file_input_resource.cc",
        "file_load_mapping.cc",
        "file_load_policy.cc",
        "file_load_rule.cc",
        "fix_reflow_filter.cc",
        "flush_html_filter.cc",
        "google_analytics_filter.cc",
        "google_font_css_inline_filter.cc",
        "google_font_service_input_resource.cc",
        "handle_noscript_redirect_filter.cc",
        "image.cc",
        "image_combine_filter.cc",
        "image_rewrite_filter.cc",
        "image_url_encoder.cc",
        "in_place_rewrite_context.cc",
        "inline_attribute_slot.cc",
        "inline_output_resource.cc",
        "inline_resource_slot.cc",
        "inline_rewrite_context.cc",
        "input_info_utils.cc",
        "insert_amp_link_filter.cc",
        "insert_dns_prefetch_filter.cc",
        "insert_ga_filter.cc",
        "javascript_code_block.cc",
        "javascript_filter.cc",
        "javascript_library_identification.cc",
        "js_combine_filter.cc",
        "js_defer_disabled_filter.cc",
        "js_disable_filter.cc",
        "js_inline_filter.cc",
        "js_outline_filter.cc",
        "js_replacer.cc",
        "lazyload_images_filter.cc",
        "local_storage_cache_filter.cc",
        "make_show_ads_async_filter.cc",
        "measurement_proxy_url_namer.cc",
        "meta_tag_filter.cc",
        "output_resource.cc",
        "pedantic_filter.cc",
        "process_context.cc",
        "property_cache_util.cc",
        "push_preload_filter.cc",
        "redirect_on_size_limit_filter.cc",
        "request_properties.cc",
        "resource.cc",
        "resource_combiner.cc",
        "resource_fetch.cc",
        "resource_namer.cc",
        "resource_slot.cc",
        "resource_tag_scanner.cc",
        "responsive_image_filter.cc",
        "rewrite_context.cc",
        "rewrite_driver.cc",
        "rewrite_driver_factory.cc",
        "rewrite_driver_pool.cc",
        "rewrite_filter.cc",
        "rewrite_gflags.cc",
        "rewrite_options.cc",
        "rewrite_query.cc",
        "rewrite_stats.cc",
        "rewritten_content_scanning_filter.cc",
        "scan_filter.cc",
        "script_tag_scanner.cc",
        "server_context.cc",
        "simple_text_filter.cc",
        "single_rewrite_context.cc",
        "srcset_slot.cc",
        "static_asset_manager.cc",
        "strip_scripts_filter.cc",
        "strip_subresource_hints_filter.cc",
        "support_noscript_filter.cc",
        "url_input_resource.cc",
        "url_left_trim_filter.cc",
        "url_namer.cc",
        "url_partnership.cc",
        "usage_data_reporter.cc",
        "webp_optimizer.cc",
        ":csp_directive_gperf",
        ":data2c_generated_add_instrumentation",
        ":data2c_generated_add_instrumentation_opt",
        ":data2c_generated_client_domain_rewriter",
        ":data2c_generated_client_domain_rewriter_opt",
        ":data2c_generated_critical_css_beacon",
        ":data2c_generated_critical_css_beacon_opt",
        ":data2c_generated_critical_css_loader",
        ":data2c_generated_critical_css_loader_opt",
        ":data2c_generated_critical_images_beacon",
        ":data2c_generated_critical_images_beacon_opt",
        ":data2c_generated_dedup_inlined_images",
        ":data2c_generated_dedup_inlined_images_opt",
        ":data2c_generated_defer_iframe",
        ":data2c_generated_defer_iframe_opt",
        ":data2c_generated_delay_images",
        ":data2c_generated_delay_images_inline",
        ":data2c_generated_delay_images_inline_opt",
        ":data2c_generated_delay_images_opt",
        ":data2c_generated_deterministic",
        ":data2c_generated_deterministic_opt",
        ":data2c_generated_extended_instrumentation",
        ":data2c_generated_extended_instrumentation_opt",
        ":data2c_generated_js_defer",
        ":data2c_generated_js_defer_opt",
        ":data2c_generated_lazyload_images",
        ":data2c_generated_lazyload_images_opt",
        ":data2c_generated_local_storage_cache",
        ":data2c_generated_local_storage_cache_opt",
        ":data2c_generated_responsive_js",
        ":data2c_generated_responsive_js_opt",
        ":rewrite_filter_names_gperf",
    ],
    hdrs = [
        "config/measurement_proxy_rewrite_options_manager.h",
        "config/rewrite_options_manager.h",
        "google_analytics_snippet.h",
        "public/add_head_filter.h",
        "public/add_ids_filter.h",
        "public/add_instrumentation_filter.h",
        "public/association_transformer.h",
        "public/base_tag_filter.h",
        "public/beacon_critical_images_finder.h",
        "public/cache_extender.h",
        "public/cacheable_resource_base.h",
        "public/collect_dependencies_filter.h",
        "public/common_filter.h",
        "public/critical_css_beacon_filter.h",
        "public/critical_finder_support_util.h",
        "public/critical_images_beacon_filter.h",
        "public/critical_images_finder.h",
        "public/critical_selector_filter.h",
        "public/critical_selector_finder.h",
        "public/csp.h",
        "public/csp_directive.h",
        "public/css_absolutify.h",
        "public/css_combine_filter.h",
        "public/css_filter.h",
        "public/css_flatten_imports_context.h",
        "public/css_hierarchy.h",
        "public/css_image_rewriter.h",
        "public/css_inline_filter.h",
        "public/css_inline_import_to_link_filter.h",
        "public/css_minify.h",
        "public/css_move_to_head_filter.h",
        "public/css_outline_filter.h",
        "public/css_resource_slot.h",
        "public/css_summarizer_base.h",
        "public/css_tag_scanner.h",
        "public/css_url_counter.h",
        "public/css_url_encoder.h",
        "public/css_url_extractor.h",
        "public/css_util.h",
        "public/data_url_input_resource.h",
        "public/debug_filter.h",
        "public/decode_rewritten_urls_filter.h",
        "public/dedup_inlined_images_filter.h",
        "public/defer_iframe_filter.h",
        "public/delay_images_filter.h",
        "public/dependency_tracker.h",
        "public/deterministic_js_filter.h",
        "public/device_properties.h",
        "public/dom_stats_filter.h",
        "public/domain_lawyer.h",
        "public/domain_rewrite_filter.h",
        "public/downstream_cache_purger.h",
        "public/downstream_caching_directives.h",
        "public/experiment_matcher.h",
        "public/experiment_util.h",
        "public/fake_filter.h",
        "public/file_input_resource.h",
        "public/file_load_mapping.h",
        "public/file_load_policy.h",
        "public/file_load_rule.h",
        "public/fix_reflow_filter.h",
        "public/flush_html_filter.h",
        "public/google_analytics_filter.h",
        "public/google_font_css_inline_filter.h",
        "public/google_font_service_input_resource.h",
        "public/handle_noscript_redirect_filter.h",
        "public/image.h",
        "public/image_combine_filter.h",
        "public/image_data_lookup.h",
        "public/image_rewrite_filter.h",
        "public/image_url_encoder.h",
        "public/in_place_rewrite_context.h",
        "public/inline_attribute_slot.h",
        "public/inline_output_resource.h",
        "public/inline_resource_slot.h",
        "public/inline_rewrite_context.h",
        "public/input_info_utils.h",
        "public/insert_amp_link_filter.h",
        "public/insert_dns_prefetch_filter.h",
        "public/insert_ga_filter.h",
        "public/javascript_code_block.h",
        "public/javascript_filter.h",
        "public/javascript_library_identification.h",
        "public/js_combine_filter.h",
        "public/js_defer_disabled_filter.h",
        "public/js_disable_filter.h",
        "public/js_inline_filter.h",
        "public/js_outline_filter.h",
        "public/js_replacer.h",
        "public/lazyload_images_filter.h",
        "public/local_storage_cache_filter.h",
        "public/make_show_ads_async_filter.h",
        "public/measurement_proxy_url_namer.h",
        "public/meta_tag_filter.h",
        "public/output_resource.h",
        "public/output_resource_kind.h",
        "public/pedantic_filter.h",
        "public/process_context.h",
        "public/property_cache_util.h",
        "public/push_preload_filter.h",
        "public/redirect_on_size_limit_filter.h",
        "public/request_properties.h",
        "public/resource.h",
        "public/resource_combiner.h",
        "public/resource_fetch.h",
        "public/resource_namer.h",
        "public/resource_slot.h",
        "public/resource_tag_scanner.h",
        "public/responsive_image_filter.h",
        "public/rewrite_context.h",
        "public/rewrite_driver.h",
        "public/rewrite_driver_factory.h",
        "public/rewrite_driver_pool.h",
        "public/rewrite_filter.h",
        "public/rewrite_gflags.h",
        "public/rewrite_options.h",
        "public/rewrite_query.h",
        "public/rewrite_result.h",
        "public/rewrite_stats.h",
        "public/rewritten_content_scanning_filter.h",
        "public/scan_filter.h",
        "public/script_tag_scanner.h",
        "public/server_context.h",
        "public/simple_text_filter.h",
        "public/single_rewrite_context.h",
        "public/srcset_slot.h",
        "public/static_asset_manager.h",
        "public/strip_scripts_filter.h",
        "public/strip_subresource_hints_filter.h",
        "public/support_noscript_filter.h",
        "public/url_input_resource.h",
        "public/url_left_trim_filter.h",
        "public/url_namer.h",
        "public/url_partnership.h",
        "public/usage_data_reporter.h",
        "public/webp_optimizer.h",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":rewriter_protos_cc",
        "//net/instaweb:net_instaweb_lib",
        "//net/instaweb/http",
        "//net/instaweb/spriter",
        "//net/instaweb/util",
        "//pagespeed/controller",
        "//pagespeed/kernel/base:pagespeed_base",
        "//pagespeed/kernel/cache",
        "//pagespeed/kernel/image",
        "//pagespeed/kernel/js",
        "//pagespeed/opt/ads",
        "//third_party/css_parser",
    ],
)
